package branch_access;

import java.io.IOException;
import mware_lib.RemoteException;
import mware_lib.namensdienst.communication.Client;
import mware_lib.namensdienst.messages.ManReqMsg;
import mware_lib.namensdienst.messages.ManRetMsg;

public class ManagerProxy extends Manager{

	private Client client;
	private String host;
	private int port;
	
	public ManagerProxy(String host, int port, String iD) {
		this.host = host;
		this.port = port;
		
		//System.out.println("ManagerProxy host: " + host);
		//System.out.println("ManagerProxy port: " + port);
	}


	@Override
	public String createAccount(String owner) {
		String id = "";
		
		try {
			client = new Client(host, port);
			//System.out.println("managerProxy schickt createAccount");
			client.send(new ManReqMsg("createAccount", owner, null));
			ManRetMsg backMsg = (ManRetMsg)client.receive();
			id =  backMsg.getKontoID();
			client.close();
		} catch (IOException e) {
			throw new RemoteException(e.getClass().toString() + ": " + e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			throw new RemoteException(e.getClass().toString() + ": " + e.getMessage(), e);
		}
		
		return id;
	}


	@Override
	public boolean removeAccount(String accountID) {
		boolean removed = false;
		
		try {
			client = new Client(host, port);
			client.send(new ManReqMsg("removeAccount", null, accountID));
			ManRetMsg backMsg = (ManRetMsg)client.receive();
			removed =  backMsg.isRemoved();
			client.close();
		} catch (IOException e) {
			throw new RemoteException(e.getClass().toString() + ": " + e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			throw new RemoteException(e.getClass().toString() + ": " + e.getMessage(), e);
		}
		
		return removed;
	}
	
}
